package com.veng.springbootdemo.service;

import com.google.common.io.Resources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class MailServerImp implements MailService {

    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public void sendMail(String to, String subject, String content) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//        设置发送方
        simpleMailMessage.setFrom(from);
//        设置接收方
        simpleMailMessage.setTo(to);
//        设置邮件主题
        simpleMailMessage.setSubject(subject);
//        设置邮件内容
        simpleMailMessage.setText(content);
        javaMailSender.send(simpleMailMessage);
        System.out.println("邮件已发送");
    }

    @Override
    public void sendHtmlMail(String to, String subject, String content) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            //true表示需要创建一个multipart message, 后面设置 setText 的时候，也要加上 true
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(content,true);
            javaMailSender.send(mimeMessage);
            System.out.println("html 邮件发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void sendAttachmentsMail(String to, String subject, String content,String filePath) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            //这句一定要加上,true
            mimeMessageHelper.setText(content,true);
//            FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
            File file = new File(Resources.getResource(filePath).getFile());
            String filename = file.getName();
            mimeMessageHelper.addAttachment(filename,file);
            javaMailSender.send(mimeMessage);
            System.out.println("附件已发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(content,true);
            File file = new File(Resources.getResource(rscPath).getFile());
            mimeMessageHelper.addInline(rscId,file);
            javaMailSender.send(mimeMessage);
            System.out.println("图片邮件已发送");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    @Autowired
    private TemplateEngine templateEngine;
    @Override
    public void sendTemplateMail(String to,String subject,String id) {
        Context context = new Context();
        context.setVariable("id",id);
        String emailTemplate = templateEngine.process("emailTemplate", context);
        sendHtmlMail(to,subject,emailTemplate);
        System.out.println("模板邮件已发送");
    }
}
